/*
 * Copyright 2009 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package simple.runtime.components.impl.android;

import android.view.View;
import android.view.ViewGroup;
import simple.runtime.components.布局;
import simple.runtime.components.组件;
import simple.runtime.components.组件容器;
import simple.runtime.components.面板;
import simple.runtime.variants.ObjectVariant;
import simple.runtime.variants.Variant;

/**
 * 可放置在窗口或其他容器中的容器。
 *
 * @author Herbert Czymontek
 */
public class 面板Impl extends 视图组件 implements 面板, 视图组件容器 {

  // 布局
  protected 布局Impl viewLayout;

  // 当这是真时，尝试更改布局属性会导致错误
  private boolean layoutFixed;
  
  // 布局属性的支持
  private Variant layout;
  // 背景颜色属性的支持
  private int backgroundColor;

  /**
   * 创建一个新的面板组件。
   *
   * @param container  容器、组件将放入
   */
  public 面板Impl(组件容器 container) {
    super(container);

    layoutFixed = false;
  }

  @Override
  protected View createView() {
    return null;
  }

  // 面板 实现

  @Override
  public int 背景颜色() {
    return backgroundColor;
  }

  @Override
  public void 背景颜色(int argb) {
    backgroundColor = argb;
    // 由于Simple属性的排序，尚未设置ViewLayout。
    if (viewLayout != null) {
      viewLayout.getLayoutManager().setBackgroundColor(argb);
    }
  }

  @Override
  public Variant 布局() {
    return layout;
  }

  @Override
  public void 布局(Variant layoutType) {
    if (layoutFixed) {
      throw new IllegalStateException("将子组件添加到其容器后，布局属性不能更改");
    }

    switch (layoutType.getInteger()) {
      default:
        throw new IllegalArgumentException("未知布局");

      case 组件.布局_线性:
        viewLayout = new 线性布局Impl(this);
        break;

      case 组件.布局_表格:
        viewLayout = new 表格布局Impl(this);
        break;

      case 组件.布局_单帧:
        viewLayout = new 单帧布局Impl(this);
        break;
    }
    layout = ObjectVariant.getObjectVariant(viewLayout);

    背景颜色(backgroundColor);

    addToContainer();
  }

  protected void addToContainer() {
    // 无法在ViewComponent构造函数中提前添加此组件 - 现在执行它
    getComponentContainer().addComponent(this);
  }

  // 组件容器 实现

  @Override
  public ViewGroup getLayoutManager() {
    return viewLayout.getLayoutManager();
  }

  @Override
  public void addComponent(组件 component) {
    // 现在已添加组件，防止布局更改
    layoutFixed = true;

    viewLayout.addComponent((视图组件) component);
  }

  @Override
  public 布局 getLayout() {
    return viewLayout;
  }

  // 视图组件 实现

  @Override
  public View getView() {
    return viewLayout == null ? null : viewLayout.getLayoutManager();
  }
}